don't build/run doctests when target != host
authorJorge Aparicio <japaricious@gmail.com>
Tue, 15 Mar 2016 00:00:39 +0000 (19:00 -0500)
committerJorge Aparicio <japaricious@gmail.com>
Tue, 15 Mar 2016 00:01:56 +0000 (19:01 -0500)
fixes rust-lang/rust#31907

src/cargo/ops/cargo_test.rs
tests/test_cargo_cross_compile.rs

index 55a9ec5f08fa97997b6e975ac3a56af1b5fcabc8..d4d20fea7d7060286acbae5c2134c6e7bd12672c 100644 (file)
@@ -113,6 +113,13 @@ fn run_doc_tests(options: &TestOptions,
     let mut errors = Vec::new();
     let config = options.compile_opts.config;
 
+    // We don't build/rust doctests if target != host
+    if let Some(target) = options.compile_opts.target {
+        if config.rustc_info().host != target {
+            return Ok(errors);
+        }
+    }
+
     let libs = compilation.to_doc_test.iter().map(|package| {
         (package, package.targets().iter().filter(|t| t.doctested())
                          .map(|t| (t.src_path(), t.name(), t.crate_name())))
index a2dc833e0e17de8e0cc1e99d17c301ef5e298129..9cad506cbd6b1e9d245137d2b26c7b4788eba684 100644 (file)
@@ -46,6 +46,21 @@ fn alternate_arch() -> &'static str {
     }
 }
 
+fn host() -> String {
+    let platform = match env::consts::OS {
+        "linux" => "unknown-linux-gnu",
+        "macos" => "apple-darwin",
+        "windows" => "pc-windows-msvc",
+        _ => unreachable!(),
+    };
+    let arch = match env::consts::ARCH {
+        "x86" => "i686",
+        "x86_64" => "x86_64",
+        _ => unreachable!(),
+    };
+    format!("{}-{}", arch, platform)
+}
+
 test!(simple_cross {
     if disabled() { return }
 
@@ -474,6 +489,63 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
    doctest = DOCTEST)));
 });
 
+test!(no_cross_doctests {
+    if disabled() { return }
+
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            authors = []
+            version = "0.0.0"
+        "#)
+        .file("src/lib.rs", r#"
+            //! ```
+            //! extern crate foo;
+            //! assert!(true);
+            //! ```
+        "#);
+
+    let host_output = format!("\
+{compiling} foo v0.0.0 ({foo})
+{running} target[..]foo-[..]
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
+
+{doctest} foo
+
+running 1 test
+test _0 ... ok
+
+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
+
+", compiling = COMPILING, running = RUNNING, foo = p.url(), doctest = DOCTEST);
+
+    assert_that(p.cargo_process("test"),
+                execs().with_status(0)
+                       .with_stdout(&host_output));
+
+    let target = host();
+    assert_that(p.cargo_process("test").arg("--target").arg(&target),
+                execs().with_status(0)
+                       .with_stdout(&host_output));
+
+    let target = alternate();
+    assert_that(p.cargo_process("test").arg("--target").arg(&target),
+                execs().with_status(0)
+                       .with_stdout(&format!("\
+{compiling} foo v0.0.0 ({foo})
+{running} target[..]{triple}[..]foo-[..]
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
+
+", compiling = COMPILING, running = RUNNING, foo = p.url(), triple = target)));
+});
+
 test!(simple_cargo_run {
     if disabled() { return }